package baseTest;

public class search {
    public static void main(String[] args) {
        int[] nums = {8,1,2,3,4,5,6,7};
        System.out.println(process(nums,6));
    }
    public static int  process(int[] nums,int target){
        if (nums == null || nums.length < 1){
            return -1;
        }
        return process(nums,0,nums.length-1,target);
    }
    public static int process(int[] nums ,int l,int r,int target){
        if (l >= r){
            return -1;
        }
        if ( nums[l] == target ||nums[r] == target){
            return nums[l] == target? l:r;
        }
        int end =-1;
        int mid = l+((r-l) >> 1);
        if (target == nums[mid]){
            return mid;
        }
        if (nums[mid] < target){
            if (nums[l] <nums[r]){
                end =  process(nums,mid+1,r,target);
            }else if (nums[mid] > nums[r] && nums[mid] < nums[mid+1] || target<nums[l]){
               end =  process(nums,mid+1,r,target);
            } else {
                end =process(nums,l,mid-1,target);
            }
        }
        if (nums[mid] > target ){
            if (nums[l] < nums[r]){
                end =  process(nums,l,mid-1,target);
            }else if (nums[mid] < nums[r] && nums[mid] >nums[mid-1] || target > nums[l]){
                end =  process(nums,l,mid-1,target);
            } else {
                end =  process(nums,mid+1,r,target);
            }

        }
        return end;
    }


}
